function loadJS(url, callback){
    var script = document.createElement('script'),
    fn = callback || function(){};
    script.type = 'text/javascript';
    //IE
    if(script.readyState){
        script.onreadystatechange = function(){
            if(script.readyState == 'loaded' || script.readyState == 'complete' ){
                script.onreadystatechange = null;
                fn();
            }
        };
    }else{
        //其他浏览器
        script.onload = function(){
            fn();
        };
    }
    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
}
function getScriptInnerText(res){
    var result="";
    res.replace(/<script.*?>([\s\S]+?)<\/script>/,function(_,js){    //正则匹配出script中的内容
        result = js
    });
    return result;
}
function getHTML(res){
    return res.match(/<div.*>[\s\S]*?<\/div>/)[0];
}
function excuteJS(code){  
    if(!code) return
    loadJS('https://cdn.bootcdn.net/ajax/libs/require.js/2.3.6/require.js',
    ()=>{
        console.log("sucess load require.js")
        let plotDom = document.createElement('script');
        plotDom.type = 'text/javascript';
        plotDom.text = `requirejs.config({
            paths: {
            plotly: ['https://cdn.plot.ly/plotly-latest.min'],
            },
        });
        if (!window.Plotly) {
            require(['plotly'], function(plotly) {
            window.Plotly = plotly;
            });
        }`;
        document.body.appendChild(plotDom);

        let scriptDom = document.createElement('script');
        scriptDom.type = 'text/javascript';
        scriptDom.text = code
        document.body.appendChild(scriptDom)
        console.log("success plot")
    })
}
export default{
    getScriptInnerText,
    getHTML,
    excuteJS
}